package cn.flying.cloud.flow.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.flying.cloud.flow.model.ProcHisInstance;
import cn.flying.cloud.flow.model.ProcInstance;


/**
 * 工作流底层表查询通过接口
 *
 * @author: admin
 * @date: 2023年02月16日 16:34
 * @version: 1.0
 */
@Mapper
public interface FlowableCommonMapper {
    /**
     * 流程执行实例查询
     *
     * @param
     * @return
     */
    @Select("<script> select " +
            "a.proc_def_id_, " +
            "a.proc_inst_id_, " +
            "a.business_key_, " +
            "a.suspension_state_, " +
            "a.start_time_, " +
            "a.start_user_id_, " +
            "a.tenant_id_, " +
            "b.id_ execution_id_, " +
            "c.name_ proc_def_name_, " +
            "c.deployment_id_, " +
            "c.description_, " +
            "c.key_ proc_def_key_, " +
            "e.assignee_,  " +
            "e.id_ task_id_,  " +
            "e.name_  node_name_, " +
            "d.act_type_ node_type_, " +
            "e.task_def_key_  node_id, " +
            "e.form_key_  form_key_ " +
            "from " +
            "act_ru_execution a " +
            "inner join act_ru_execution b on a.proc_inst_id_ = a.id_  " +
            "and a.proc_inst_id_ = b.proc_inst_id_  " +
            "and b.id_ != b.proc_inst_id_  " +
            "and b.tenant_id_ = a.tenant_id_ " +
            "inner join act_re_procdef c on a.proc_def_id_ = c.id_  " +
            "and c.tenant_id_ = a.tenant_id_ " +
            "inner join act_ru_actinst d on b.act_id_ = d.act_id_ " +
            "and b.proc_inst_id_ = d.proc_inst_id_  " +
            "and b.tenant_id_ = d.tenant_id_ " +
            "left join act_ru_task e on a.proc_inst_id_ = e.proc_inst_id_ and a.tenant_id_ = e.tenant_id_ " +
            "<where>" +
            "<if test='bizKey != null and bizKey != \"\"'> and a.business_key_ = #{bizKey} </if>" +
            "<if test='procDefKey != null and procDefKey != \"\"'> and c.key_ = #{procDefKey} </if>" +
            "<if test='procDefId != null and procDefId != \"\"'> and a.proc_def_id_ = #{procDefId} </if>" +
            "<if test='procInstId != null and procInstId != \"\"'> and a.proc_inst_id_ = #{procInstId} </if>" +
            "<if test='tenantId != null and tenantId != \"\"'> and a.tenant_id_ = #{tenantId} </if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "executionId", column = "execution_id_"),
            @Result(property = "procDefId", column = "proc_def_id_"),
            @Result(property = "procDefKey", column = "proc_def_key_"),
            @Result(property = "procDefName", column = "proc_def_name_"),
            @Result(property = "procInstId", column = "proc_inst_id_"),
            @Result(property = "suspensionState", column = "suspension_state_"),
            @Result(property = "bizKey", column = "business_key_"),
            @Result(property = "deploymentId", column = "deployment_id_"),
            @Result(property = "description", column = "description_"),
            @Result(property = "assignee", column = "assignee_"),
            @Result(property = "taskId", column = "task_id_"),
            @Result(property = "nodeName", column = "node_name_"),
            @Result(property = "nodeType", column = "node_type_"),
            @Result(property = "nodeId", column = "node_id"),
            @Result(property = "formKey", column = "form_key_"),
            @Result(property = "tenantId", column = "tenant_id_"),
            @Result(property = "startTime", column = "start_time_"),
            @Result(property = "startUserId", column = "start_user_id_"),
    })
    Page<ProcInstance> getProcInstanceList(@Param("page") Page<ProcInstance> page, @Param("bizKey") String bizKey, @Param("procDefKey") String procDefKey, @Param("procDefId") String procDefId, @Param("procInstId") String procInstId, @Param("tenantId") String tenantId);


    /**
     * 流程执行实例历史查询
     *
     * @param
     * @return
     */
    @Select("<script> select " +
            "a.proc_def_id_, " +
            "a.proc_inst_id_, " +
            "a.business_key_, " +
            "a.start_time_, " +
            "a.end_time_, " +
            "a.duration_, " +
            "a.start_user_id_, " +
            "a.tenant_id_, " +
            "a.delete_reason_, " +
            "a.business_status_, " +
            "b.name_ proc_def_name_, " +
            "b.key_ proc_def_key_ " +
            "from " +
            "act_hi_procinst a " +
            "inner join act_hi_actinst b " +
            "on a.proc_inst_id_ = b.proc_inst_id_  " +
            "<where>" +
            "<if test='param.bizKey != null and param.bizKey != \"\"'> and a.business_key_ = #{param.bizKey} </if>" +
            "<if test='param.procDefKey != null and param.procDefKey != \"\"'> and b.key_ = #{param.procDefKey} </if>" +
            "<if test='param.procDefId != null and param.procDefId != \"\"'> and a.proc_def_id_ = #{param.procDefId} </if>" +
            "<if test='param.procInstId != null and param.procInstId != \"\"'> and a.proc_inst_id_ = #{param.procInstId} </if>" +
            "<if test='param.startUserId != null and param.startUserId != \"\"'> and a.start_user_id_ = #{param.startUserId} </if>" +
            "<if test='param.tenantId != null and param.tenantId != \"\"'> and a.tenant_id_ = #{param.tenantId} </if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "procDefId", column = "proc_def_id_"),
            @Result(property = "procDefKey", column = "proc_def_key_"),
            @Result(property = "procDefName", column = "proc_def_name_"),
            @Result(property = "procInstId", column = "proc_inst_id_"),
            @Result(property = "bizKey", column = "business_key_"),
            @Result(property = "tenantId", column = "tenant_id_"),
            @Result(property = "deleteReason", column = "delete_reason_"),
            @Result(property = "startUserId", column = "start_user_id_"),
            @Result(property = "startTime", column = "start_time_"),
            @Result(property = "endTime", column = "end_time_"),
            @Result(property = "duration", column = "duration_"),
            @Result(property = "businessStatus", column = "business_status_"),
    })
    Page<ProcHisInstance> getProcHisInstanceList(@Param("page") Page<ProcInstance> page, @Param("param") ProcHisInstance instance);

    /**
     * 查询异常节点
     *
     * @param procInstId
     * @return
     */
    @Select("SELECT a.act_id_ FROM act_ru_execution a JOIN act_ru_deadletter_job b ON a.id_=b.execution_id_ AND a.tenant_id_=b.tenant_id_ WHERE a.proc_inst_id_=#{procInstId}")
    List<String> getExceptionActivityIds(String procInstId);


}
